using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RedBlueBalls
{
    public partial class Main : Form
    {
        RedBlueBallRecord[] records = null;
        bool analyzed = false;

        public Main()
        {
            InitializeComponent();
            openRecordDlg.InitialDirectory = Application.StartupPath;
            Tracer.SetTracer(this.txtMessage);
        }

        /// <summary>
        /// Import data from file to memory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, EventArgs e)
        {
           if (openRecordDlg.ShowDialog() == DialogResult.OK)
           {
               Cursor.Current = Cursors.WaitCursor;
               txtMessage.Clear();
               string filename = openRecordDlg.FileName;
               records = RecordLoader.LoadFromFile(filename);
               analyzed = false;
               Cursor.Current = Cursors.Default;
           }
        }

        /// <summary>
        /// Analyze the red blue balls result
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAnalyze_Click(object sender, EventArgs e)
        {
            txtMessage.Clear();
            if (records == null)
            {
                MessageBox.Show("Please import the data first!");
                return;
            }

            Cursor.Current = Cursors.WaitCursor;
            RedSum.analyze(records);
            RedFreq.analyze(records);
            RedByBlue.analyze(records);
            RedDate.analyze(records);
            RedOdd.analyze(records);
            RedDigit.analyze(records);
            RedPrime.analyze(records);
            RedSame.analyze(records);
            RedSegment.analyze(records);
            RedContinual.analyze(records);
            RedEdge.analyze(records);
            Tracer.ShowMessage();
            analyzed = true;
            Cursor.Current = Cursors.Default;
        }

        /// <summary>
        /// Predict the result
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPredict_Click(object sender, EventArgs e)
        {
            txtMessage.Clear();
            if (!analyzed)
            {
                MessageBox.Show("Please analyze the data first!");
                return;
            }

            Cursor.Current = Cursors.WaitCursor;
            int count = 0;
            RedBlueBallRecord last = records[records.Length-1];
            for (int i1 = 1; i1 < 29; i1++)
            {
                for (int i2 = i1 + 1; i2 < 30; i2++)
                {
                    for (int i3 = i2 + 1; i3 < 31; i3++)
                    {
                        for (int i4 = i3 + 1; i4 < 32; i4++)
                        {
                            for (int i5 = i4 + 1; i5 < 33; i5++)
                            {
                                for (int i6 = i5 + 1; i6 < 34; i6++)
                                {
                                    RedBlueBallRecord candidate = new RedBlueBallRecord(i1, i2, i3, i4, i5, i6, 0);
                                    if (RedSum.predict(candidate) && RedOdd.predict(candidate)
                                        && RedPrime.predict(candidate)
                                        && RedDigit.predict(candidate)
                                        && RedDelta.predict(candidate) && RedFreq.predict(candidate)
                                        && RedSame.predict(candidate) && RedContinual.predict(candidate)
                                        && RedEdge.predict(candidate))
                                    {
                                        count++;
                                        Tracer.AppendMessage(candidate.ToString());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Tracer.AppendMessage("Total Record:" + count.ToString());
            Tracer.ShowMessage();
            Cursor.Current = Cursors.Default;
        }

        /// <summary>
        /// Clear output message
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtMessage.Clear();
        }
    }
}